const _ = require('lodash')

function getArea(r) {
    console.log(r)
    return Math.PI * Math.pow(r, 2)
}

function add1(a) {
    console.log('输出', a)
    return a + 1
}
// const addMomory = _.memoize(add1)
// console.log(addMomory(10))
// console.log(addMomory(10))
// console.log(addMomory(10))

// const getAreaWithMemory = _.memoize(getArea) // 记忆函数

// console.log(getAreaWithMemory(10))
// console.log(getAreaWithMemory(10))
// console.log(getAreaWithMemory(10))

function memoize(fn) {
    const cache = {}

    return (...rest) => {
        const key = JSON.stringify(rest)
        console.log(cache)
        cache[key] = cache[key] || fn.apply(fn, rest)
        return cache[key]
    }
}

const addMomory = _.memoize(add1)
console.log(addMomory(10))
console.log(addMomory(10))
console.log(addMomory(10))

const getAreaWithMemory = memoize(getArea) // 记忆函数
const getAdd1Memory = memoize(add1)

// console.log(getAreaWithMemory(10))
// console.log(getAreaWithMemory(10))
// console.log(getAreaWithMemory(10))
// console.log(getAdd1Memory(10))
// console.log(getAdd1Memory(10))


